package com.rh.number.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.NoArgsConstructor;
import java.util.Collections;
import java.util.List;

@NoArgsConstructor(force = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class StatsPageResult<T> {
    private final int code;
    private final String msg;
    private final List<T> data;
    private final int currentPage;
    private final int pageSize;
    private final long totalElements;
    private final int totalPages;

    // Getters
    public int getCode() { return code; }
    public String getMsg() { return msg; }
    public List<T> getData() { return data; }
    public int getCurrentPage() { return currentPage; }
    public int getPageSize() { return pageSize; }
    public long getTotalElements() { return totalElements; }
    public int getTotalPages() { return totalPages; }

    private StatsPageResult(Builder<T> builder) {
        this.code = builder.code;
        this.msg = builder.msg;
        this.data = builder.data;
        this.currentPage = builder.currentPage;
        this.pageSize = builder.pageSize;
        this.totalElements = builder.totalElements;
        this.totalPages = builder.totalPages;
    }

    public static class Builder<T> {
        private int code = 200;
        private String msg = "success";
        private List<T> data = Collections.emptyList();
        private int currentPage = 1;
        private int pageSize = 10;
        private long totalElements = 0;
        private int totalPages = 0;

        public Builder<T> code(int code) {
            this.code = code;
            return this;
        }

        public Builder<T> msg(String msg) {
            this.msg = msg;
            return this;
        }

        public Builder<T> data(List<T> data) {
            this.data = data != null ? data : Collections.emptyList();
            return this;
        }

        public Builder<T> currentPage(int currentPage) {
            this.currentPage = Math.max(currentPage, 1);
            return this;
        }

        public Builder<T> pageSize(int pageSize) {
            this.pageSize = Math.min(Math.max(pageSize, 1), 200);
            return this;
        }

        public Builder<T> totalElements(long totalElements) {
            this.totalElements = Math.max(totalElements, 0);
            return this;
        }

        public StatsPageResult<T> build() {
            this.totalPages = (int) Math.ceil((double) totalElements / pageSize);
            return new StatsPageResult<>(this);
        }
    }

    public static <T> StatsPageResult<T> success(List<T> data,
                                                 int page,
                                                 int size,
                                                 long total) {
        return new Builder<T>()
                .data(data)
                .currentPage(page)
                .pageSize(size)
                .totalElements(total)
                .build();
    }

    public static <T> StatsPageResult<T> error(int code, String msg) {
        return new Builder<T>()
                .code(code)
                .msg(msg)
                .build();
    }
}